package cn.stylefeng.guns.modular.ry600.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stylefeng.guns.modular.system.model.Clientproduct;
import cn.stylefeng.guns.modular.system.dao.ClientcustomerMapper;
import cn.stylefeng.guns.modular.system.dao.ClientproductMapper;
import cn.stylefeng.guns.modular.ry600.service.IClientproductService;
import cn.stylefeng.roses.core.mutidatasource.annotion.DataSource;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-13
 */
@Service
public class ClientproductServiceImpl extends ServiceImpl<ClientproductMapper, Clientproduct> implements IClientproductService {
	@Autowired
	private ClientproductMapper clientproductMapper;
	
	@Override
	public Object selectByMsg(Map<String, Object> msg) {
		if (MapUtils.isNotEmpty(msg)) {
			String[] likeFields = { "clientName","productName"};
			for (String field : likeFields) {
				if (msg.containsKey(field)) {
					msg.put(field, "%" + (String) msg.get(field) + "%");
				}
			}
			if (msg.containsKey("dataType")) {
				msg.put("dataType", (String) msg.get("dataType") + "%");
			}
		}
		
		return clientproductMapper.selectByMsg(msg);
	}

	@Override
	public int delClientPro(String clientId) {
		return clientproductMapper.delClientPro(clientId);
	}

	@Override
	public int getCount(Map<String, Object> msg) {
		if (MapUtils.isNotEmpty(msg)) {
			String[] likeFields = { "clientName","productName"};
			for (String field : likeFields) {
				if (msg.containsKey(field)) {
					msg.put(field, "%" + (String) msg.get(field) + "%");
				}
			}
			if (msg.containsKey("dataType")) {
				msg.put("dataType", (String) msg.get("dataType") + "%");
			}
		}
		return clientproductMapper.getCount(msg);
	}

	@Override
	@DataSource(name = "#dataSource")
	public int insertBatch(List<Clientproduct> clientproducts, String dataSource) {
		// TODO Auto-generated method stub
		Map<String,Object> msg=new HashMap<String,Object>();
		int count=0;
		for(int i=0;i<clientproducts.size();i++){
			msg.put("clientId", clientproducts.get(i).getClientId());
			msg.put("productId", clientproducts.get(i).getProductId());
			List<Clientproduct> returnPro=clientproductMapper.selectByMsg(msg);
			if(returnPro.size()==0){
				boolean bl=this.insert(clientproducts.get(i));
				if(bl){
					count++;
				}
			}
			msg.clear();
		}
		return count;
	}

	@Override
	@DataSource(name = "#dataSource")
	public int delByDao(Clientproduct clientproduct, String dataSource) {
		// TODO Auto-generated method stub
		return clientproductMapper.delByDao(clientproduct);
	}
}
